package cn.csust.net2.manager.client.panel;

import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.client.ux.BeanGridPanel;
import cn.csust.net2.manager.shared.po.Activity;
import cn.csust.net2.manager.shared.po.Comprehensive;
import cn.csust.net2.manager.shared.po.Exam;
import cn.csust.net2.manager.shared.po.HeldPosition;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.PartyTraining;
import cn.csust.net2.manager.shared.po.Punishment;
import cn.csust.net2.manager.shared.po.RatifyJoinParty;
import cn.csust.net2.manager.shared.po.Reward;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.StudentInformationService;
import cn.csust.net2.manager.shared.service.StudentInformationServiceAsync;
import cn.csust.net2.manager.shared.vo.Config;

import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.TabPanel.TabPosition;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;

public class StudentInformationPanel extends ContentPanel {
	private TabPanel tabPanel;
	private TabItem rewardTabItem;
	private TabItem punishmentTabItem;
	private TabItem heldPositionTabItem;
	private TabItem activityTabItem;
//	private TabItem politicalTabItem;
//	private TabItem poorInfoTabItem;
	private TabItem examTabItem;
	private TabItem comprehensiveTabItem;
	private TabItem partyTrainingTabItem;
	private TabItem ratifyJoinPartyTabItem;
	protected StudentInformationServiceAsync service;
	
	private BeanGridPanel<Reward> rewardGridPanel;
	private BeanGridPanel<Punishment> punishmentGridPanel;
	private BeanGridPanel<HeldPosition> heldPositionGridPanel;
	private BeanGridPanel<Activity> activityGridPanel;
	private BeanGridPanel<Exam> examGridPanel;
	private BeanGridPanel<Comprehensive> comprehensiveGridPanel;
	private BeanGridPanel<PartyTraining> partyTrainingGridPanel;
	private BeanGridPanel<RatifyJoinParty> ratifyJoinPartyGridPanel;
	private boolean editable = true;		//默认是可编辑的
	private Student student ;		//默认是当前登录的用户
	
	public StudentInformationPanel() {
		setHeaderVisible(false);
		student = (Student)App.getUser();
		service = GWT.create(StudentInformationService.class);
		create();
	
	}
	
	public StudentInformationPanel(Student student, boolean editable) {
		setHeaderVisible(false);
		this.student = student;
		this.editable = editable;
		service = GWT.create(StudentInformationService.class);
		create();
		
	}
	
	@Override
	protected void onRender(Element parent, int pos) {
		// TODO Auto-generated method stub
		super.onRender(parent, pos);
		this.setLayout(new FitLayout());
		
	}

	public void create() {
		tabPanel = new TabPanel();
		tabPanel.setTabPosition(TabPosition.BOTTOM); 

		//奖励
		
		rewardTabItem = new TabItem("奖励");
		rewardGridPanel = new BeanGridPanel<Reward>(new Reward(),service);
		Config loadConfig = new Config();
		loadConfig.put("student", student);
		tabPanel.add(createTabWithGridPanel(rewardTabItem, rewardGridPanel, loadConfig));
		
		//处分
		punishmentTabItem = new TabItem("处分");
		punishmentGridPanel = new BeanGridPanel<Punishment>(new Punishment(),service);
		tabPanel.add(createTabWithGridPanel(punishmentTabItem, punishmentGridPanel, loadConfig));	
		
		//担任职位
		heldPositionTabItem = new TabItem("担任职位");
		heldPositionGridPanel = new BeanGridPanel<HeldPosition>(new HeldPosition(),service);
		tabPanel.add(createTabWithGridPanel(heldPositionTabItem, heldPositionGridPanel, loadConfig));	
		
		//参加校内外活动
		activityTabItem = new TabItem("参加校内外活动");
		activityGridPanel = new BeanGridPanel<Activity>(new Activity(),service);
		tabPanel.add(createTabWithGridPanel(activityTabItem, activityGridPanel, loadConfig));
		
		//过级
		examTabItem = new TabItem("过级");
		examGridPanel = new BeanGridPanel<Exam>(new Exam(),service);
		tabPanel.add(createTabWithGridPanel(examTabItem, examGridPanel, loadConfig));	
		
		//综合测评
		comprehensiveTabItem = new TabItem("综合测评");
		comprehensiveGridPanel = new BeanGridPanel<Comprehensive>(new Comprehensive(),service);
		tabPanel.add(createTabWithGridPanel(comprehensiveTabItem, comprehensiveGridPanel, loadConfig));
		
		
		//党校培训
		partyTrainingTabItem = new TabItem("党校学习");
		partyTrainingGridPanel = new BeanGridPanel<PartyTraining>(new PartyTraining(),service);
		tabPanel.add(createTabWithGridPanel(partyTrainingTabItem, partyTrainingGridPanel, loadConfig));
		
		//党校培训
		ratifyJoinPartyTabItem = new TabItem("党校学习");
		ratifyJoinPartyGridPanel = new BeanGridPanel<RatifyJoinParty>(new RatifyJoinParty(),service);
		tabPanel.add(createTabWithGridPanel(ratifyJoinPartyTabItem, ratifyJoinPartyGridPanel, loadConfig));
				
		this.add(tabPanel);
	}
	
	/**
	 * 创建一个TabItem，里面装在一个GridPanel，并根据loadConfig加载表格的数据
	 * @param tabItem
	 * @param gridPanel
	 * @param loadConfig
	 * @return
	 */
	public TabItem createTabWithGridPanel(TabItem tabItem, BeanGridPanel<? extends PO> gridPanel, Config loadConfig) {
		tabItem.setLayout(new FitLayout());
		gridPanel.setItemId(tabItem.getText());
		gridPanel.setTermConfig(true);
		if(!editable) {
			gridPanel.setAddButton(false);
			gridPanel.setModifyButton(false);
			gridPanel.setDeleteButton(false);
			gridPanel.setSaveButton(false);
			gridPanel.setEditAble(false);
		}else{
			gridPanel.setEditAble(true);
		}
		gridPanel.createDefault();
		gridPanel.load(loadConfig);
		tabItem.add(gridPanel);
		return tabItem;
		
	}

	public TabPanel getTabPanel() {
		return tabPanel;
	}

	public void setTabPanel(TabPanel tabPanel) {
		this.tabPanel = tabPanel;
	}

	public TabItem getRewardTabItem() {
		return rewardTabItem;
	}

	public void setRewardTabItem(TabItem rewardTabItem) {
		this.rewardTabItem = rewardTabItem;
	}

	public TabItem getPunishmentTabItem() {
		return punishmentTabItem;
	}

	public void setPunishmentTabItem(TabItem punishmentTabItem) {
		this.punishmentTabItem = punishmentTabItem;
	}

	public TabItem getHeldPositionTabItem() {
		return heldPositionTabItem;
	}

	public void setHeldPositionTabItem(TabItem heldPositionTabItem) {
		this.heldPositionTabItem = heldPositionTabItem;
	}

	public TabItem getActivityTabItem() {
		return activityTabItem;
	}

	public void setActivityTabItem(TabItem activityTabItem) {
		this.activityTabItem = activityTabItem;
	}

	public TabItem getExamTabItem() {
		return examTabItem;
	}

	public void setExamTabItem(TabItem examTabItem) {
		this.examTabItem = examTabItem;
	}

	public TabItem getComprehensiveTabItem() {
		return comprehensiveTabItem;
	}

	public void setComprehensiveTabItem(TabItem comprehensiveTabItem) {
		this.comprehensiveTabItem = comprehensiveTabItem;
	}

	public StudentInformationServiceAsync getService() {
		return service;
	}

	public void setService(StudentInformationServiceAsync service) {
		this.service = service;
	}

	public BeanGridPanel<Reward> getRewardGridPanel() {
		return rewardGridPanel;
	}

	public void setRewardGridPanel(BeanGridPanel<Reward> rewardGridPanel) {
		this.rewardGridPanel = rewardGridPanel;
	}

	public BeanGridPanel<Punishment> getPunishmentGridPanel() {
		return punishmentGridPanel;
	}

	public void setPunishmentGridPanel(BeanGridPanel<Punishment> punishmentGridPanel) {
		this.punishmentGridPanel = punishmentGridPanel;
	}

	public BeanGridPanel<HeldPosition> getHeldPositionGridPanel() {
		return heldPositionGridPanel;
	}

	public void setHeldPositionGridPanel(
			BeanGridPanel<HeldPosition> heldPositionGridPanel) {
		this.heldPositionGridPanel = heldPositionGridPanel;
	}

	public BeanGridPanel<Activity> getActivityGridPanel() {
		return activityGridPanel;
	}

	public void setActivityGridPanel(BeanGridPanel<Activity> activityGridPanel) {
		this.activityGridPanel = activityGridPanel;
	}

	public BeanGridPanel<Exam> getExamGridPanel() {
		return examGridPanel;
	}

	public void setExamGridPanel(BeanGridPanel<Exam> examGridPanel) {
		this.examGridPanel = examGridPanel;
	}

	public BeanGridPanel<Comprehensive> getComprehensiveGridPanel() {
		return comprehensiveGridPanel;
	}

	public void setComprehensiveGridPanel(
			BeanGridPanel<Comprehensive> comprehensiveGridPanel) {
		this.comprehensiveGridPanel = comprehensiveGridPanel;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
}
